import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// 导入组件
import login from '@/views/login/index.vue'
import layout from '@/views/layout/index.vue'
import dashboard from '@/views/dashboard/index.vue'
import article from '@/views/article/index.vue'

Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/layout',
    component: layout,
    children: [
      { path: '/', redirect: '/dashboard' },
      { path: '/dashboard', component: dashboard },
      { path: '/article', component: article }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前置全局路由导航守卫
router.beforeEach((to, from, next) => {
  // 登录权限: 有权限就正常跳转,没有权限就跳转登录页面
  // 可以跳转 : 有token || 跳转登录页
  // console.log(store)
  // console.log(store.state.user.Token)
  if (store.state.user.Token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
})

export default router
